const zeroLine = function(n) {
    let result = []
    for (let i = 0; i < n; i++) {
        result.push(0)
    }
    return result
}
const createMap = function(width, height = width) {
    let result = []
    for (let i = 0; i < height; i++) {
        result.push(zeroLine(width))
    }
    return result
}
const loadMap = function(arr) {
    let container = e('#id-div-container')
    let t = mapTemplate(arr)
    prependHtml(container, t)
}
const reloadMap = function(arr) {
    let map = findMap()
    map.remove()
    loadMap(arr)
}
const loadPanel = function(block, score, lineNumber) {
    let container = e('#id-div-container')
    let state = block.state
    let blockArray = block.block[state].map(items => items.map(item => item * block.color))
    let t = panelTemplate(blockArray, score, lineNumber)
    appendHtml(container, t)
}
const updateNextBlock = function(block) {
    let container = e('#id-div-next-block-container')
    let state = block.state
    let blockArray = block.block[state].map(items => items.map(item => item * block.color))
    let t = mapTemplate(blockArray, 'id-div-next-block-map')
    container.innerHTML = t
}
const updateScore = function(score) {
    let scoreDiv = e('#id-div-score')
    scoreDiv.innerHTML = score
}
const updateLineNumber = function(lineNumber) {
    let linesDiv = e('#id-div-lines')
    linesDiv.innerHTML = lineNumber
}
const updateLevel = function(level) {
    let levelDiv = e('#id-div-level')
    levelDiv.innerHTML = level
}
const updatePanel = function(block, score, lineNumber, level) {
    updateNextBlock(block)
    updateScore(score)
    updateLineNumber(lineNumber)
    updateLevel(level)
}
const initScene = function(game) {
    loadMap(game.map)
    loadPanel(game.nextBlock, game.score, game.lineNumber, game.level)
}
const reload = function(game) {
    reloadMap(game.map)
    updatePanel(game.nextBlock, game.score, game.lineNumber, game.level)
}
const findMap = function() {
    let container = e('#id-div-container')
    return container.querySelector('#id-div-map')
}